package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduMember;
import com.jf3q.common.domain.EduOrder;
import com.jf3q.common.utils.SaltPassUtils;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.dto.MemberLoginDto;
import com.jf3q.front.dto.UserPasswordDTO;
import com.jf3q.front.service.MemberService;
import com.jf3q.front.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Member;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    MemberService memberService;

    @Autowired
    OrderService orderService;

    //更改方法（昵称）
    @PostMapping("/save")
    public ResultVo save(@RequestBody EduMember member) {
        memberService.saveOrUpdate(member);
        return ResultVo.success("操作成功！", member);
    }

    //修改密码
    @PostMapping("/password/{mobile}")
    public ResultVo Password(@RequestBody  MemberLoginDto memberLoginDto, @PathVariable String mobile) {
        EduMember eduMember=new EduMember();
        byte[] salt = SaltPassUtils.generateSalt();   //生成一个随机的盐值
        String saltStr = Base64.getEncoder().encodeToString(salt);  //将盐转换成Base64编码的字符
        eduMember.setSalt(saltStr);   //将盐值放入到数据的Salt中
        eduMember.setPassword(SaltPassUtils.hashPassword(memberLoginDto.getNewPassword(), salt));//密码需要加盐加密的
        memberService.update(eduMember, new LambdaQueryWrapper<EduMember>().eq(EduMember::getMobile, mobile));
        return ResultVo.success("修改密码成功", null);
    }

    //
    @PostMapping("/page")
    public ResultVo page(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = Integer.MAX_VALUE + "") Integer pageSize,
                         @RequestBody EduOrder eduOrder,
                          String mobile) {
        LambdaQueryWrapper<EduOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(eduOrder.getOrderNo())) {
            queryWrapper.eq(EduOrder::getOrderNo, eduOrder.getOrderNo());
        }
        if (!ObjectUtils.isEmpty(eduOrder.getStatus())) {
            queryWrapper.like(EduOrder::getStatus, eduOrder.getStatus());
        }

        if (!ObjectUtils.isEmpty(mobile)){
            queryWrapper.eq(EduOrder::getMobile,mobile);
        }

        Page<EduOrder> page = orderService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResultVo.success("查询订单成功！", page);
    }

    @GetMapping("/checkOldPwd")
    public ResultVo checkOldPwd(String password, String mobile) {
        return orderService.checkOldPwd(password,mobile);
    }

    //验证方法
    }
